const app = getApp();
Page({


  data: {
    index: null,
    warehouse: [],
    allwarehouse: [], //查询的仓库
    selected: [], //选中的仓库
    use_id: '',
    tokens: '',
  },
  //选择仓库
  PickerChange(e) {
    // console.log(e.detail);
    this.setData({
      index: e.detail.value,
    })
    let warehouseid = Number(e.detail.value) + 1
    this.getwarehouse_detils(this.data.tokens, this.data.use_id, warehouseid) //查看仓库详情
  },

  onLoad: function (options) {
    var user_id = wx.getStorageSync('user_id')
    var token = wx.getStorageSync('token')
    this.setData({
      use_id: user_id,
      tokens: token,
    })
  },


  onReady: function () {

  },


  onShow: function () {
    this.getwarehouse() //登录前仓库列表
    if (this.data.use_id != null) {
      this.logingwarehouse(this.data.use_id, this.data.tokens) //登陆后仓库列表
    }
  },
  //未登陆时展示的查询页面
  getwarehouse() {
    wx.request({
      url: `${app.globalData.api}/api/User/index`,
      method: 'POST',
      data: {},
      success: (res) => {
        console.log(res.data.data.warehouse);
        var allwarehouse = res.data.data.warehouse;
        this.setData({
          warehouse: allwarehouse
        })
      }
    })
  },

  //登录后的才可以选择查询
  logingwarehouse(token, user_id) {
    wx.request({
      url: `${app.globalData.api}/api/User/warehouse_list`,
      method: 'POST',
      data: {
        token: token,
        user_id: user_id,
      },
      success: (res) => {
        console.log("登陆才能查看的", res.data.data);
        this.setData({
          warehouse_t:res.data.data.warehouse
        })
      }
    })
    this.getwarehouse_detils(1,1,1) //没登录时初始化详情
  },
  //获取仓库详情
  getwarehouse_detils(tokens, use_id, id) {
    wx.request({
      url: `${app.globalData.api}/api/User/warehouse_info`,
      method: 'POST',
      data: {
        token: tokens,
        user_id: use_id,
        ware_id: id
      },
      success: (res) => {
        console.log("选择的仓库", res.data.data);
        this.setData({
          selected: res.data.data
        })
      }
    })
  }


})